package com.xiaoxi.exam.utils;

import cn.xinzhili.xutils.core.util.HashUtils;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;

/**
 * 使用SMTP协议发送电子邮件
 */
@Slf4j
public class MailUtil {


  // 邮件发送协议
  private final static String PROTOCOL = "smtp";

  // SMTP邮件服务器
  private final static String HOST = "smtp.163.com";

  // SMTP邮件服务器默认端口
  private final static String PORT = "465";

  // 是否要求身份认证
  private final static String IS_AUTH = "true";

  // 是否启用调试模式（启用调试模式可打印客户端与服务器交互过程时一问一答的响应消息）
  private final static String IS_ENABLED_DEBUG_MOD = "false";

  // 发件人
  private static String from = "liuhao_cg@163.com";

  // 收件人
  private static String to = "18519337879@163.com";

  // 初始化连接邮件服务器的会话信息
  private static Properties props = null;
  private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

  static {
    props = new Properties();
    props.setProperty("mail.transport.protocol", PROTOCOL);
    props.setProperty("mail.smtp.host", HOST);
    props.setProperty("mail.smtp.port", PORT);
    props.setProperty("mail.smtp.auth", IS_AUTH);
    props.setProperty("mail.debug", IS_ENABLED_DEBUG_MOD);
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.port", PORT);
  }


  public static boolean sendSSLMail(String username, String password, String tel, String city,
      String school, String date, String orderId, String url) {
    try {
      //设置SSL连接、邮件环境
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

      //建立邮件会话
      Session session = Session.getDefaultInstance(props, new Authenticator() {
        //身份认证
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication("liuhao_cg@163.com", "Lh19891224");
        }
      });
      // 创建MimeMessage实例对象
      MimeMessage message = new MimeMessage(session);
      // 设置发件人
      message.setFrom(new InternetAddress(from));
      // 设置邮件主题
      message.setSubject("托福报考考位放出");
      // 设置收件人
      message.setRecipient(RecipientType.TO, new InternetAddress(to));
      message.setRecipient(RecipientType.CC, new InternetAddress(from));
      // 设置发送时间
      message.setSentDate(new Date());
      // 设置纯文本内容为邮件正文
      String result =
          "用户名:" + username + ", 密码:" + password + ", 手机:" + tel + ", 报考城市:" + city + ", 报考学校:"
              + school + ", 报考时间:" + date
              + ", 报考成功后用浏览器访问以下网址完成注册：" + url + "?orderId=" + HashUtils.decode(orderId)
              + "&school=" + school.split(" ")[1] + "&date=" + date + "&tel=" + tel;
      message.setText(result);
      // 保存并生成最终的邮件内容
      message.saveChanges();

      // 获得Transport实例对象
      Transport transport = session.getTransport();
      // 打开连接
      transport.connect("liuhao_cg@163.com", "Liu19891224");
      // 将message对象传递给transport对象，将邮件发送出去
      transport.sendMessage(message, message.getAllRecipients());
      // 关闭连接
      transport.close();
      log.info("main sent message is {}", result);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }


}
